import asyncio
import json
from EdgeGPT import Chatbot, ConversationStyle
from utils.wechart import wechat
import re

async def bing(prompt):
    with open('cookies.json', 'r') as f:
        cookies = json.load(f)
    bot = Chatbot(cookies=cookies)
    #  creative 有创造力的、 precise 准确的、 balanced 平衡的
    reply = await bot.ask(prompt, conversation_style=ConversationStyle.precise,wss_link="wss://sydney.bing.com/sydney/ChatHub")
     # get reply text
    text = f"{reply['item']['messages'][1]['text']}"
    superscript_map = {'0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴', '5': '⁵', '6': '⁶', '7': '⁷', '8': '⁸', '9': '⁹'}
    text = re.sub(r'\[\^(\d+)\^\]', lambda match: ''.join(superscript_map.get(digit, digit) for digit in match.group(1)), text)
    await bot.close()
    return text
    
def EdgeGptanswer(text):
    answer = asyncio.run(bing(text))
    print("回答：" + answer)
    wechat.send_text_message(answer) 

if __name__ == "__main__":
    # asyncio.run(bing("你是谁"))
    EdgeGptanswer("你是谁")
